from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer

from coures.models import TeacherModel, CourseModel, CommModel


class TeacherSer(ModelSerializer):
    class Meta:
        model=TeacherModel
        fields='__all__'

class CourseSer(ModelSerializer):
    tea=SerializerMethodField(read_only=True)
    def get_tea(self,obj):
        return {"name":obj.teacher.name,"job":obj.teacher.job,"headimg":obj.teacher.headimg,"intro":obj.teacher.intro}
    class Meta:
        model=CourseModel
        fields='__all__'

class CommSer(ModelSerializer):
    npm=SerializerMethodField(read_only=True)
    def get_npm(self,obj):
        return obj.userCommentsModel.phone
    class Meta:
        model=CommModel
        fields='__all__'

from drf_haystack.serializers import HaystackSerializer
from coures import search_indexes
class CourseIndexSerializer(HaystackSerializer):
    class Meta:
        index_classes = [search_indexes.CourseIndex]
        fields = ['text','name','teacher','intro']
